package com.clms.rents.service.impl;

import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.clms.common.R;
import com.clms.rents.mapper.TabCarBrand;
import com.clms.rents.pojo.entity.TbBrand;
import com.clms.rents.pojo.vo.CarBrandVo;
import com.clms.rents.service.CarBrandService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @program: clms-third
 * @description: 车品牌业务层
 * @author: 杨浩
 * @create: 2021-07-22 12:05
 **/

@Service
public class CarBrandServiceimpl implements CarBrandService {


    @Resource
    private TabCarBrand tabCarBrand;

    @Override
    public R selectall() {
        List<TbBrand> tbBrands = tabCarBrand.selectList(null);
        return R.ok().put("allBrands",tbBrands);

    }

    @Override
    public R pageSelect(Integer currentPage, Integer pageSize, CarBrandVo carBrandVo) {
        Page<TbBrand> page=new Page<>(currentPage,pageSize);
        QueryWrapper<TbBrand> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(carBrandVo.getName())){
            queryWrapper.like("name",carBrandVo.getName());
        }
        Page<TbBrand> page1=tabCarBrand.selectPage(page,queryWrapper);
        return R.ok().put("total",page1.getTotal()).put("records",page1.getRecords());
    }

    @Override
    public R insert(TbBrand tbBrand) {
        int insert = tabCarBrand.insert(tbBrand);

        return R.ok();
    }

    @Override
    public R update(TbBrand tbBrand) {

        int i = tabCarBrand.updateById(tbBrand);
        return R.ok();
    }

    @Override
    public R selectById(Integer id) {
        TbBrand tbBrand = tabCarBrand.selectById(id);


        return R.ok().put("huixian",tbBrand);
    }

    @Override
    public R delete(Integer id) {
        int i = tabCarBrand.deleteById(id);
        return R.ok();
    }

    @Override
    public R selectByName(CarBrandVo carBrandVo) {
        QueryWrapper<TbBrand> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(carBrandVo.getName())){
            queryWrapper.eq("name",carBrandVo.getName());
        }
        if(StringUtils.isNotEmpty(carBrandVo.getId())){
            queryWrapper.ne("id",carBrandVo.getId());
        }
       if(StringUtils.isNotEmpty(carBrandVo.getName())){
           TbBrand tbBrand = tabCarBrand.selectOne(queryWrapper);
           if(tbBrand!=null){
               return R.error().message("已经存在");
           }else {
               return R.ok().message("");
           }
       }
         else {
           return   R.error().message("不能为空");
       }



    }
}
